/**
 * Created by zhangxiaoyu on 16/9/20.
 */
angular.module('hyt.hzKaiDanCtrl',[])

    .controller('HzKaiDanCtrl',function($scope,$state,ENV,Storage,$stateParams,$timeout,$filter,FProCitAreFactory,SProCitAreFactory,AddCydFactory,saveFhdFactory,$ionicLoading){
        var sid=Storage.get("sid");
        var yh_id=Storage.get("yh_id");
        var user_id=Storage.get('user_id');
        var lxdh=Storage.get('lxdh');
        Storage.set('buDan',null);
        $scope.fhd={
            fhrmc:'',
            fhrdh:'',
            fhrdz:'',
            shrmc:'',
            shrdh:'',
            shrdz:'',
            hwmc:'',
            sl:'',
            zl:'',
            tj:'',
            bjsm:'',
            dshk:'',
            isTb:'',
            isHd:'',
            bz:''
        };
        if (Storage.get('fhd')!=null){
            $scope.fhd=Storage.get('fhd');
            $('.bjsmDot').val($scope.fhd.bjsm);
            $('.dshkDot').val($scope.fhd.dshk);
            $('.zlDot').val($scope.fhd.zl);
            $('.tjDot').val($scope.fhd.tj);
            $scope.fhrq=Storage.get('fhrq');
            if (Storage.get('provinceF')!='地区选择'){
                $scope.citiesF=Storage.get('citiesDataF');
                $scope.areasF=Storage.get('areasDataF');
                $timeout(function(){
                    $('#provinceF').val(Storage.get('provinceF'));
                    $('#cityF').val(Storage.get('cityF'));
                    $('#areaF').val(Storage.get('areaF'));
                },300);
            }
            if (Storage.get('provinceS')!='地区选择'){
                $scope.citiesS=Storage.get('citiesDataS');
                $scope.areasS=Storage.get('areasDataS');
                $timeout(function(){
                    $('#provinceS').val(Storage.get('provinceS'));
                    $('#cityS').val(Storage.get('cityS'));
                    $('#areaS').val(Storage.get('areaS'));
                },300);
            }
            $timeout(function(){
                $('#shType').val(Storage.get('hzShfs'));
                if ($stateParams.khmc!=''||$stateParams.lxdh!=''||$stateParams.address!=''){
                    $scope.fhd.shrmc=$stateParams.khmc;
                    $scope.fhd.shrdh=$stateParams.lxdh;
                    $scope.fhd.shrdz=$stateParams.address;
                }
            },200);
        }
        //发货日期
        $scope.fhrq =$filter('date')(new Date(),'yyyy-MM-dd');
        $scope.showStartDate = function() { //选择日期
            var optionsStart = {
                date:new Date(),
                mode: 'date'
            };
            datePicker.show(optionsStart, function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        $scope.fhrq = $filter('date')(d,'yyyy-MM-dd');
                    });
                }
            });
        };
        if (Storage.get('provincesData')!=null){
            $scope.provinces=Storage.get('provincesData');
        }else {
            FProCitAreFactory.getPro(sid,user_id,yh_id);
        }
        $scope.$on('FPro.proUpdated',function(){
            $scope.provinces=FProCitAreFactory.getP();
            Storage.set('provincesData',$scope.provinces);
        });
        $scope.$on('FCity.cityUpdated',function(){
            $scope.citiesF=FProCitAreFactory.getC();
            FProCitAreFactory.getAre(sid,user_id,yh_id,$scope.citiesF[0].code);
            Storage.set('citiesDataF',$scope.citiesF);
        });
        $scope.$on('FArea.areaUpdated',function(){
            $scope.areasF=FProCitAreFactory.getA();
            Storage.set('areasDataF',$scope.areasF);
        });
        $scope.$on('SCity.cityUpdated',function(){
            $scope.citiesS=SProCitAreFactory.getC();
            SProCitAreFactory.getAre(sid,user_id,yh_id,$scope.citiesS[0].code);
            Storage.set('citiesDataS',$scope.citiesS);
        });
        $scope.$on('SArea.areaUpdated',function(){
            $scope.areasS=SProCitAreFactory.getA();
            Storage.set('areasDataS',$scope.areasS);
        });
        $("#provinceF").change(function(e) {
            FProCitAreFactory.getCity(sid,user_id,yh_id,$('#provinceF').val());
            if ($('#provinceF').val()=='地区选择'){
                $timeout(function(){
                    $scope.citiesF=null;
                    $scope.areasF=null;
                },300);
            }
        });
        $("#cityF").change(function(e) {
            FProCitAreFactory.getAre(sid,user_id,yh_id,$('#cityF').val());
        });
        $("#provinceS").change(function(e) {
            SProCitAreFactory.getCity(sid,user_id,yh_id,$('#provinceS').val());
            if ($('#provinceS').val()=='地区选择'){
                $timeout(function(){
                    $scope.citiesS=null;
                    $scope.areasS=null;
                },300);
            }
        });
        $("#cityS").change(function(e) {
            SProCitAreFactory.getAre(sid,user_id,yh_id,$('#cityS').val());
        });
        AddCydFactory.queryShfs(sid,yh_id,user_id);
        $scope.$on('InitData',function(){
            $scope.shfs=AddCydFactory.getShfs();
        });
        $(".zlDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(3));
            $scope.fhd.zl=$(this).val();
            if($scope.fhd.zl>3000000){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "重量不可超过3000000吨",
                    duration: 1500
                });
                $("#zl").css("color","red");
            }else{
                $("#zl").css("color","black");
            }
        });
        $(".tjDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(3));
            $scope.fhd.tj=$(this).val();
            if($scope.fhd.tj>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "体积不可超过3000000立方米",
                  duration: 1500
                });
                $("#tj").css("color","red");
            }else{
                $("#tj").css("color","black");
            }
        });
        $(".bjsmDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.fhd.bjsm=$(this).val();
            if($scope.fhd.bjsm>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $(".bjsmDot").css("color","red");
            }else{
                $(".bjsmDot").css("color","black");
            }
        });
        $(".dshkDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.fhd.dshk=$(this).val();
            if($scope.fhd.dshk>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $(".dshkDot").css("color","red");
            }else{
                $(".dshkDot").css("color","black");
            }
        });
        $scope.chooseShr=function(htmlbs){
            Storage.set('fhrq',$scope.fhrq);
            Storage.set('provinceF',$('#provinceF option:selected').val());
            Storage.set('cityF',$('#cityF option:selected').val());
            Storage.set('areaF',$('#areaF option:selected').val());
            Storage.set('provinceS',$('#provinceS option:selected').val());
            Storage.set('cityS',$('#cityS option:selected').val());
            Storage.set('areaS',$('#areaS option:selected').val());
            Storage.set('hzShfs',$('#shType option:selected').val());
            $timeout(function(){
                Storage.set('fhd',$scope.fhd);
            },300);
            $state.go('hzShrList',{htmlbs:htmlbs});
        };
        $scope.changejian=function(a){
          if(a.toString().length>=7){
            $ionicLoading.show({
              noBackdrop: true,
              template: "件数最大限制7位数",
              duration: 1500
            });
          }
        };
        //保存发货单
        $scope.saveFhd=function(){
            var fproCode=$('#provinceF').val();
            var fcityCode=$('#cityF').val();
            var fareaCode=$('#areaF').val();
            var sproCode=$('#provinceS').val();
            var scityCode=$('#cityS').val();
            var sareaCode=$('#areaS').val();
            var fpro=$('#provinceF option:selected').text();
            var fcity=$('#cityF option:selected').text();
            var farea=$('#areaF option:selected').text();
            var spro=$('#provinceS option:selected').text();
            var scity=$('#cityS option:selected').text();
            var sarea=$('#areaS option:selected').text();
            var shfs=$('#shType option:selected').val();
            $("#save").attr("disabled", true);
            Storage.set('fhd',null);
            if ($('#provinceF option:selected').text()=='地区选择'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请选择发货地区",
                    duration: 600
                });
                $("#save").attr("disabled", false);
            }
            else if ($('#provinceS option:selected').text()=='地区选择'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请选择收货地区",
                    duration: 600
                });
                $("#save").attr("disabled", false);
            }else {
                $timeout(function(){
                  saveFhdFactory.getFhd(sid,user_id,yh_id,$scope.fhd,$scope.fhrq,fpro,fcity,farea,spro,scity,sarea,fproCode,fcityCode,fareaCode,sproCode,scityCode,sareaCode,shfs,lxdh,1,1);
                },160);
                $scope.$on('Save.fhdUpdated',function(){
                    var result=saveFhdFactory.getResult();
                    if (result.result==true){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "保存成功",
                            duration: 600
                        });
                        $("#save").attr("disabled", false);
                        $state.go('hzcydlist');
                    }
                });
            }
        };
        //提交发货单
        $scope.submitFhd=function(){
            var fproCode=$('#provinceF').val();
            var fcityCode=$('#cityF').val();
            var fareaCode=$('#areaF').val();
            var sproCode=$('#provinceS').val();
            var scityCode=$('#cityS').val();
            var sareaCode=$('#areaS').val();
            var fpro=$('#provinceF option:selected').text();
            var fcity=$('#cityF option:selected').text();
            var farea=$('#areaF option:selected').text();
            var spro=$('#provinceS option:selected').text();
            var scity=$('#cityS option:selected').text();
            var sarea=$('#areaS option:selected').text();
            var shfs=$('#shType option:selected').val();
            var fhrCity=$('#cityF option:selected').text();
            $("#fahuo").attr("disabled", true);
            Storage.set('fhd',null);
            if ($('#provinceF option:selected').text()=='地区选择'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请选择发货地区",
                    duration: 600
                });
                $("#fahuo").attr("disabled", false);
            }
            else if ($('#provinceS option:selected').text()=='地区选择'){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请选择收货地区",
                    duration: 600
                });
                $("#fahuo").attr("disabled", false);
            }else {
                $timeout(function(){
                    saveFhdFactory.getFhd(sid,user_id,yh_id,$scope.fhd,$scope.fhrq,fpro,fcity,farea,spro,scity,sarea,fproCode,fcityCode,fareaCode,sproCode,scityCode,sareaCode,shfs,lxdh,1,1);
                },160);
                $scope.$on('Save.fhdUpdated',function(){
                    var result=saveFhdFactory.getResult();
                    if (result.result==true){
                        $("#fahuo").attr("disabled", false);
                        $state.go('company',{fhd_id:result.fhd_id,fhrdh:$scope.fhd.fhrdh,fhrCity:fhrCity});
                    }
                });
            }
        };
    })
    //选择物流公司
    .controller('ChooseWlgsCtrl',function($scope,$state,ENV,Storage,$stateParams,$timeout,$filter,LszdFactory,CityzdFactory,OftenzdFactory,AllzdFactory,$ionicHistory,$ionicPopup,$window,addCompanyFactory,$ionicLoading,submitFhdFactory){
        $scope.showloading=true;
        var sid=Storage.get("sid");
        var yh_id=Storage.get("yh_id");
        var user_id=Storage.get('user_id');
        var fhd_id=$stateParams.fhd_id;
        var fhrdh=$stateParams.fhrdh;
        var lxdh=Storage.get('lxdh');
        if ($stateParams.fhrCity!=undefined){
            var fhrCity=$stateParams.fhrCity.replace(/市/,'');
        }
        $scope.allzdData='';
        $scope.search={
            contentLs:'',
            contentCity:'',
            contentOften:'',
            contentAll:''
        };
        LszdFactory.getLsfhzd(sid,user_id,yh_id,lxdh,$scope.search.contentLs);
        $scope.$on('LszdList.lszdUpdated',function(){
            var result=LszdFactory.getLszd();
            $scope.lszdData=result.data;
            $scope.showloading=false;
        });
        CityzdFactory.getCityzd(sid,user_id,yh_id,fhrCity,$scope.search.contentCity);
        $scope.$on('CityzdList.cityzdUpdated',function(){
            var result=CityzdFactory.getCity();
            $scope.cityzdData=result.data;
            console.log($scope.cityzdData);
            $scope.showloading=false;
        });
        OftenzdFactory.getOftenzd(sid,user_id,yh_id,$scope.search.contentOften);
        $scope.$on('OftenzdList.oftenzdUpdated',function(){
            var result=OftenzdFactory.getOften();
            $scope.oftenzdData=result.data;
            console.log($scope.oftenzdData);
            $scope.showloading=false;
        });
        $scope.searchLs=function(){
            LszdFactory.getLsfhzd(sid,user_id,yh_id,lxdh,$scope.search.contentLs);
        };
        $scope.searchCity=function(){
            CityzdFactory.getCityzd(sid,user_id,yh_id,fhrCity,$scope.search.contentCity);
        };
        $scope.searchOften=function(){
            OftenzdFactory.getOftenzd(sid,user_id,yh_id,$scope.search.contentOften);
        };
        $scope.searchAll=function(){
            if ($scope.search.contentAll!=''){
                AllzdFactory.getAllzd(sid,user_id,yh_id,$scope.search.contentAll);
                $scope.$on('AllzdList.allzdUpdated',function(){
                    var result=AllzdFactory.getAll();
                    $scope.allzdData=result.data;
                    console.log($scope.allzdData);
                    $scope.showloading=false;
                });
            }else {
                $scope.allzdData='';
            }
        };
        //下拉更新
        $scope.doRefreshLs=function(){
            LszdFactory.getLsfhzd(sid,user_id,yh_id,lxdh,$scope.search.contentLs);
            $scope.$broadcast('scroll.refreshComplete');
        };
        $scope.doRefreshCity=function(){
            CityzdFactory.getCityzd(sid,user_id,yh_id,fhrCity,$scope.search.contentCity);
            $scope.$broadcast('scroll.refreshComplete');
        };
        $scope.doRefreshOften=function(){
            OftenzdFactory.getOftenzd(sid,user_id,yh_id,$scope.search.contentOften);
            $scope.$broadcast('scroll.refreshComplete');
        };
        $scope.doRefreshAll=function(){
            AllzdFactory.getAllzd(sid,user_id,yh_id,$scope.search.contentAll);
            $scope.$broadcast('scroll.refreshComplete');
        };
        //上拉更新
        $scope.loadMoreLs=function(){
            LszdFactory.getMoreLszd(sid,user_id,yh_id,lxdh,$scope.search.contentLs);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        $scope.loadMoreCity=function(){
            CityzdFactory.getMoreCityzd(sid,user_id,yh_id,fhrCity,$scope.search.contentCity);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        $scope.loadMoreOften=function(){
            OftenzdFactory.getMoreOftenzd(sid,user_id,yh_id,$scope.search.contentOften);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        $scope.loadMoreAll=function(){
            AllzdFactory.getMoreAllzd(sid,user_id,yh_id,$scope.search.contentAll);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        //下一页
        $scope.hasNextPageLs = function() {
            return LszdFactory.hasNextPage();
        };
        $scope.hasNextPageCity = function() {
            return CityzdFactory.hasNextPage();
        };
        $scope.hasNextPageOften = function() {
            return OftenzdFactory.hasNextPage();
        };
        $scope.hasNextPageAll = function() {
            return AllzdFactory.hasNextPage();
        };

        $scope.selectTab=function(){
            OftenzdFactory.getOftenzd(sid,user_id,yh_id,$scope.search.contentOften);
        };
        $scope.chooseCompany=function(zdxx){
            var receipt = $ionicPopup.confirm({
                title : '是否提交发货单',
                cancelText : '否',
                cancelType : 'button-stable',
                okText : '是'
            });
            receipt.then(function(r){
                if(r){
                    submitFhdFactory.getSubmitFhd(sid,user_id,yh_id,lxdh,fhd_id,zdxx,2);
                    $scope.$on('Submit.fhdUpdated',function(){
                        var result=submitFhdFactory.getResult();
                        if (result==1){
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "发货成功",
                                duration: 1500
                            });
                            $state.go('hzcydlist');
                        }
                    });
                }else{
                    console.log('取消');
                }
            });
        };

        $scope.addCompany=function(zdxx,lybs){
            var receipt = $ionicPopup.confirm({
                title : '是否添加物流公司',
                cancelText : '否',
                cancelType : 'button-stable',
                okText : '是'
            });
            receipt.then(function(r){
                if(r){
                    addCompanyFactory.getAddCompany(sid,user_id,yh_id,zdxx);
                    $scope.$on('Add.companyUpdated',function(){
                        var result=addCompanyFactory.getResult();
                        if (result==true){
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "添加成功",
                                duration: 1500
                            });
                            if (lybs==1){
                                $scope.cityzdData.splice($scope.cityzdData.indexOf(zdxx), 1);
                            }else if (lybs==2){
                                LszdFactory.getLsfhzd(sid,user_id,yh_id,lxdh,$scope.search.contentLs);
                            }else if (lybs==3){
                                $scope.allzdData.splice($scope.allzdData.indexOf(zdxx), 1);
                            }
                        }
                    });
                }else{
                    console.log('取消');
                }
            });
        };
        $scope.callPhone = function (mobilePhone) {
            var confirmPopup = $ionicPopup.confirm({
                title:mobilePhone,
                okText:'呼叫',
                cancelText:'取消'
            });
            confirmPopup.then(function(res) {
                if (res) {
                    $window.location.href = "tel:" + mobilePhone;
                }else {

                }
            });

        };
        $scope.chooseZddz=function(data){
            $state.go('hzdzList',{dw_id:data.dw_id,zd_id:data.zd_id});
        };
        $scope.myGoBack = function() {
            $ionicHistory.goBack();
        };
        $scope.deleteCmp=function(data){
            var receipt = $ionicPopup.confirm({
                title : '是否删除物流公司',
                cancelText : '取消',
                cancelType : 'button-stable',
                okText : '删除'
            });
            receipt.then(function(r){
                if(r){
                    OftenzdFactory.getDelete(sid,user_id,yh_id,data.id);
                    $scope.$on('DeleteWlgs',function(){
                        var result=OftenzdFactory.deleteResult();
                        if (result==1){
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "删除成功",
                                duration: 1500
                            });
                            OftenzdFactory.getOftenzd(sid,user_id,yh_id,$scope.search.contentOften);
                        }else {
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "删除失败",
                                duration: 1500
                            });
                        }
                    });
                }else{
                    console.log('取消');
                }
            });
        }

    })
    .controller('HzBuDanCtrl',function($scope,$state,ENV,Storage,$stateParams,$timeout,$filter,AddCydFactory,$ionicPopup,FProCitAreFactory,SProCitAreFactory,$ionicLoading,saveBdFactory,$cordovaCamera,$ionicActionSheet,$cordovaImagePicker){
        var sid=Storage.get("sid");
        var yh_id=Storage.get("yh_id");
        var user_id=Storage.get('user_id');
        var lxdh=Storage.get('lxdh');
        $scope.buDan ={
            cydh: '',
            dwmc:'',
            thf:'',
            shrmc: '',
            shrdh: '',
            shrdz: '',
            fhrmc: '',
            fhrdh: '',
            fhrdz: '',
            hwmc: '',
            sl: '',
            zl: '',
            tj: '',
            bzlx: '',
            bz: '',
            yf:'',
            yf_xf: '',
            yf_hf: '',
            yf_yj: '',
            yf_tf: '',
            yf_ycdk: '',
            xxf_xf: '',
            xxf_qf: '',
            dsk_hf: '',
            shf: '',
            be: '',
            bf: '',
            isCk:'',
            tbzt:'',
            isHd:'',
            sfhd:'',
            yf_df: '',
            dsk_xf: ''
        };
        if (Storage.get('buDan')!=null){
            $scope.buDan=Storage.get('buDan');
            $(".zlDot").val($scope.buDan.zl);
            $(".tjDot").val($scope.buDan.tj);
            $(".numDot").val($scope.buDan.yf);
            $(".xfDot").val($scope.buDan.yf_xf);
            $(".tfDot").val($scope.buDan.yf_tf);
            $(".hfDot").val($scope.buDan.yf_hf);
            $(".yjDot").val($scope.buDan.yf_yj);
            $(".dkDot").val($scope.buDan.yf_ycdk);
            $(".dshkDot").val($scope.buDan.dsk_hf);
            $(".xxfDot").val($scope.buDan.xxf_xf);
            $(".qfDot").val($scope.buDan.xxf_qf);
            $(".shfDot").val($scope.buDan.shf);
            $(".thfDot").val($scope.buDan.thf);
            $(".beDot").val($scope.buDan.be);
            $(".bfDot").val($scope.buDan.bf);
            $(".yfdfDot").val($scope.buDan.yf_df);
            $(".dskdfDot").val($scope.buDan.dsk_xf);
            $scope.cyrq=Storage.get('cyrq');
            $scope.imgData=Storage.get('imgData');
            if (Storage.get('provincef')!='地区选择'){
                $scope.citiesF=Storage.get('citiesDataf');
                $scope.areasF=Storage.get('areasDataf');
                $timeout(function(){
                    $('#provincef').val(Storage.get('provincef'));
                    $('#cityf').val(Storage.get('cityf'));
                    $('#areaf').val(Storage.get('areaf'));
                },300);
            }
            if (Storage.get('provinces')!='地区选择'){
                $scope.citiesS=Storage.get('citiesDatas');
                $scope.areasS=Storage.get('areasDatas');
                $timeout(function(){
                    $('#provinces').val(Storage.get('provinces'));
                    $('#citys').val(Storage.get('citys'));
                    $('#areas').val(Storage.get('areas'));
                },300);
            }
            if($scope.buDan.shf!=''&&$scope.buDan.shf!=null){
              $("#shfLab").removeClass("hide").addClass("showfy");
            }
            if($scope.buDan.thf!=''&&$scope.buDan.thf!=null){
              $("#thfLab").removeClass("hide").addClass("showfy");
            }
            if(($scope.buDan.be!=''&&$scope.buDan.be!=null)||$scope.buDan.tbzt=='1'){
              $("#beLab").removeClass("hide").addClass("showfy");
              $("#bfLab").removeClass("hide").addClass("showfy");
              $scope.buDan.isCk=true;
            }
            if($scope.buDan.bf!=''&&$scope.buDan.bf!=null){
              $("#beLab").removeClass("hide").addClass("showfy");
              $("#bfLab").removeClass("hide").addClass("showfy");
            }
            if($scope.buDan.yf_df!=''&&$scope.buDan.yf_df!=null){
              $("#yfdfLab").removeClass("hide").addClass("showfy");
            }
            if($scope.buDan.dsk_xf!=''&&$scope.buDan.dsk_xf!=null){
              $("#dskdfLab").removeClass("hide").addClass("showfy");
            }
            $timeout(function(){
                $('#shfs').val(Storage.get('bdShfs'));
                $('#fkType').val(Storage.get('hzFkfs'));
                if ($stateParams.khmc!=''||$stateParams.lxdh!=''||$stateParams.address!=''){
                    $scope.buDan.shrmc=$stateParams.khmc;
                    $scope.buDan.shrdh=$stateParams.lxdh;
                    $scope.buDan.shrdz=$stateParams.address;
                }
            },200);
        }
        //承运日期
        $scope.cyrq =$filter('date')(new Date(),'yyyy-MM-dd');
        $scope.showStartDate = function() { //选择日期
            var optionsStart = {
                date:new Date(),
                mode: 'date'
            };
            datePicker.show(optionsStart, function(d) {
                if (!isNaN(d.getTime())) {  // valid date
                    $scope.$apply(function () {
                        $scope.cyrq = $filter('date')(d,'yyyy-MM-dd');
                    });
                }
            });
        };
        $scope.changejian=function(a){
            if(a.toString().length>=7){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "件数最大限制7位数",
                  duration: 1500
                });
            }
        };
        AddCydFactory.queryFkfs(sid,yh_id,user_id);
        AddCydFactory.queryShfs(sid,yh_id,user_id);
        $scope.$on('InitData',function(){
            $scope.fkfs=AddCydFactory.getFkfs();
            $scope.shfs=AddCydFactory.getShfs();
        });
        $(".zlDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(3));
            $scope.buDan.zl=$(this).val();
            if($scope.buDan.zl>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "重量不可超过3000000吨",
                  duration: 1500
                });
                $("#zl").css("color","red");
            }else{
                $("#zl").css("color","black");
            }
        });
        $(".tjDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(3));
            $scope.buDan.tj=$(this).val();
            if($scope.buDan.tj>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "体积不可超过3000000立方米",
                  duration: 1500
                });
                $("#tj").css("color","red");
            }else{
                $("#tj").css("color","black");
            }
        });
        $(".numDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.yf=$(this).val();
            if($scope.buDan.yf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#yf").css("color","red");
            }
            else {
                $("#yf").css("color","black");
            }
        });
        $(".xfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.yf_xf=$(this).val();
            if($scope.buDan.yf_xf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#yfxf").css("color","red");
            }else{
                $("#yfxf").css("color","black");
            }
        });
        $(".tfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.yf_tf=$(this).val();
            if($scope.buDan.yf_tf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#yftf").css("color","red");
            }else{
                $("#yftf").css("color","black");
            }
        });
        $(".hfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.yf_hf=$(this).val();
            if($scope.buDan.yf_hf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#yfhf").css("color","red");
            }else{
                $("#yfhf").css("color","black");
            }
        });
        $(".yjDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.yf_yj=$(this).val();
            if($scope.buDan.yf_yj>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#yfyj").css("color","red");
            }else{
                $("#yfyj").css("color","black");
            }
        });
        $(".dkDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.yf_ycdk=$(this).val();
            if($scope.buDan.yf_ycdk>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#yfdk").css("color","red");
            }else{
                $("#yfdk").css("color","black");
            }
        });
        $(".dshkDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.dsk_hf=$(this).val();
            if($scope.buDan.dsk_hf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#dskhf").css("color","red");
            }else{
                $("#dskhf").css("color","black");
            }
        });
        $(".xxfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.xxf_xf=$(this).val();
            if($scope.buDan.xxf_xf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#xf").css("color","red");
            }else{
                $("#xf").css("color","black");
            }
        });
        $(".qfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.xxf_qf=$(this).val();
            if($scope.buDan.xxf_qf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#qf").css("color","red");
            }else{
                $("#qf").css("color","black");
            }
        });
        $(".shfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.shf=$(this).val();
            if($scope.buDan.shf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#shf").css("color","red");
            }else{
                $("#shf").css("color","black");
            }
        });
        $(".thfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.thf=$(this).val();
            if($scope.buDan.thf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#thf").css("color","red");
            }else{
                $("#thf").css("color","black");
            }
        });
        $(".beDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.be=$(this).val();
            if($scope.buDan.be>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#be").css("color","red");
            }else{
                $("#be").css("color","black");
            }
        });
        $(".bfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.bf=$(this).val();
            if($scope.buDan.bf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#bf").css("color","red");
            }else{
                $("#bf").css("color","black");
            }
        });
        $(".yfdfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.yf_df=$(this).val();
            if($scope.buDan.yf_df>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#yfdf").css("color","red");
            }else{
                $("#yfdf").css("color","black");
            }
        });
        $(".dskdfDot").on("blur",function(){
            $(this).val(parseFloat($(this).val()).toFixed(2));
            $scope.buDan.dsk_xf=$(this).val();
            if($scope.buDan.dsk_xf>3000000){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "费用金额不可超过3000000元",
                  duration: 1500
                });
                $("#dskxf").css("color","red");
            }else{
                $("#dskxf").css("color","black");
            }
        });
        $scope.chooseShr=function(htmlbs){
            Storage.set('cyrq',$scope.cyrq);
            if ($scope.imgData!=undefined){
                Storage.set('imgData',$scope.imgData);
            }else {
                Storage.set('imgData',null);
            }
            Storage.set('provincef',$('#provincef option:selected').val());
            Storage.set('cityf',$('#cityf option:selected').val());
            Storage.set('areaf',$('#areaf option:selected').val());
            Storage.set('provinces',$('#provinces option:selected').val());
            Storage.set('citys',$('#citys option:selected').val());
            Storage.set('areas',$('#areas option:selected').val());
            Storage.set('bdShfs',$('#shfs option:selected').val());
            Storage.set('hzFkfs',$('#fkType option:selected').val());
            $timeout(function(){
                Storage.set('buDan',$scope.buDan);
            },300);
            $state.go('hzShrList',{htmlbs:htmlbs});
        };
        $scope.items= [{index:1,name:'送货费'},{index:2,name:'提货费'}, {index:3,name:'保额'},{index:4,name:'保费'},{index:5,name:'运费垫付'},{index:6,name:'代收款垫付'}];
        var popupForWarehouse;
        $scope.addMenu=function(){
            popupForWarehouse = $ionicPopup.alert({
                template: '<div class="list">                '+
                '  <ion-item ng-repeat="item in items" ng-click="removefy($index,item.index)" class="item item-icon-right" id="item.index"> '+
                '    <h2 >{{item.name}}<h2>                   '+
                '  </ion-item>                                      '+
                '</div>                                      ',
                title:'请选择需要添加的费用',
                scope: $scope,
                okText:'取消'
            });
        };
        $scope.removefy= function (r,i) {
            if(i!=3&&i!=4){
                $scope.items.splice(r,1);
            }
            else if(i==3){
                $scope.items.splice(r,2);
            }else if(i==4){
                $scope.items.splice(r-1,2);
            }
            switch(i) {
                case 1:
                    $("#shfLab").removeClass("hide").addClass("showfy");
                    break;
                case 2:
                    $("#thfLab").removeClass("hide").addClass("showfy");
                    break;
                case 3:
                    $("#beLab").removeClass("hide").addClass("showfy");
                    $("#bfLab").removeClass("hide").addClass("showfy");
                    break;
                case 4:
                    $("#beLab").removeClass("hide").addClass("showfy");
                    $("#bfLab").removeClass("hide").addClass("showfy");
                    break;
                case 5:
                    $("#yfdfLab").removeClass("hide").addClass("showfy");
                    break;
                case 6:
                    $("#dskdfLab").removeClass("hide").addClass("showfy");
                    break;
                default:
                    break;
            }
            popupForWarehouse.close();
        };
        $scope.holdonRemove=function(i){
            var receipt = $ionicPopup.confirm({
                title : '是否删除此费用项',
                cancelText : '否',
                cancelType : 'button-stable',
                okText : '是'
            });
            receipt.then(function(r){
                if(r){
                    switch(i) {
                        case 1:
                            $("#shfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(0,0,{index:1,name:'送货费'});
                            $scope.buDan.shf='';
                            break;
                        case 2:
                            $("#thfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(1,0,{index:2,name:'提货费'});
                            $scope.buDan.thf='';
                            break;
                        case 3:
                            $("#beLab").removeClass("showfy").addClass("hide");
                            $("#bfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(2,0,{index:3,name:'保额'});
                            $scope.items.splice(3,0,{index:4,name:'保费'});
                            $scope.buDan.be='';
                            $scope.buDan.bf='';
                            $scope.tbzt.isCk=false;
                            break;
                        case 4:
                            $("#beLab").removeClass("showfy").addClass("hide");
                            $("#bfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(2,0,{index:3,name:'保额'});
                            $scope.items.splice(3,0,{index:4,name:'保费'});
                            $scope.buDan.be='';
                            $scope.buDan.bf='';
                            $scope.tbzt.isCk=false;
                            break;
                        case 5:
                            $("#yfdfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(4,0,{index:5,name:'运费垫付'});
                            $scope.buDan.yf_df='';
                            break;
                        case 6:
                            $("#dskdfLab").removeClass("showfy").addClass("hide");
                            $scope.items.splice(5,0,{index:6,name:'代收款垫付'});
                            $scope.buDan.dsk_xf='';
                            break;
                        default:
                            break;
                    }
                }else{
                    console.log('取消');
                }
            });
        };
        $("#fkType").change(function(e){
            var yfxf=$("#yfxf").val();
            var yfhf=$("#yfhf").val();
            var yftf=$("#yftf").val();
            var yfyj=$("#yfyj").val();
            var yfdk=$("#yfdk").val();
            if(yfxf==''||yfxf==null||yfxf==undefined||isNaN(yfxf)){
                yfxf=0;
            }
            if(yfhf==''||yfhf==null||yfhf==undefined||isNaN(yfhf)){
                yfhf=0;
            }
            if(yftf==''||yftf==null||yftf==undefined||isNaN(yftf)){
                yftf=0;
            }
            if(yfyj==''||yfyj==null||yfyj==undefined||isNaN(yfyj)){
                yfyj=0;
            }
            if(yfdk==''||yfdk==null||yfdk==undefined||isNaN(yfdk)){
                yfdk=0;
            }
            var yf=parseFloat(yfxf)+parseFloat(yftf)+parseFloat(yfyj)+parseFloat(yfhf)+parseFloat(yfdk);
            var fkType=$("#fkType option:selected").val();
            if (ENV.fwqbs == 'wh'&&fkType!=2) {
                if ($scope.buDan.yf == '' || $scope.buDan.yf == null || $scope.buDan.yf == 0 ) {
                    $('#yfdf').removeAttr("readonly");
                    $('#yfdf').css("background-color","#fff");
                }else {
                    $('#yfdf').attr("readonly", "readonly");
                    $('#yfdf').css("background-color","#F2F2F2");
                    $scope.buDan.yf_df='0';
                    $("#yfdf").val("");
                }
                if($scope.buDan.yf_df==''||$scope.buDan.yf_df==null||$scope.buDan.yf_df==0){
                    $('#yf').removeAttr("readonly");
                    $('#yf').css("background-color","#fff");
                    $('#yfxf').removeAttr("readonly");
                    $('#yfxf').css("background-color","#fff");
                    $('#yfhf').removeAttr("readonly");
                    $('#yfhf').css("background-color","#fff");
                    $('#yfyj').removeAttr("readonly");
                    $('#yfyj').css("background-color","#fff");
                    $('#yfdk').removeAttr("readonly");
                    $('#yfdk').css("background-color","#fff");
                }
                else {
                    $('#yf').attr("readonly", "readonly");
                    $('#yf').css("background-color","#F2F2F2");
                    $('#yfxf').attr("readonly", "readonly");
                    $('#yfxf').css("background-color","#F2F2F2");
                    $('#yfhf').attr("readonly", "readonly");
                    $('#yfhf').css("background-color","#F2F2F2");
                    $('#yfdk').attr("readonly", "readonly");
                    $('#yfdk').css("background-color","#F2F2F2");
                    $('#yfyj').attr("readonly", "readonly");
                    $('#yfyj').css("background-color","#F2F2F2");
                }
            }
            switch (fkType){
                case "1":
                    if(yf==0){
                        $("#yf").val($scope.buDan.yf);
                        $("#yfxf").val($scope.buDan.yf);
                    }
                    else {
                        $("#yf").val(yf);
                        $("#yfxf").val(yf);
                    }
                    $scope.buDan.yf_xf=$("#yfxf").val();
                    $scope.buDan.yf_hf='';
                    $scope.buDan.yf_tf='';
                    $scope.buDan.yf_yj='';
                    $scope.buDan.yf_ycdk='';
                    $("#yfxf").val(yf);
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $('#yfdf').attr("readonly", "readonly");
                        $('#yfdf').css("background-color","#F2F2F2");
                        $scope.buDan.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
                case "2":
                    if(yf==0){
                        $("#yf").val($scope.buDan.yf);
                        $("#yftf").val($scope.buDan.yf);
                    }
                    else {
                        $("#yf").val(yf);
                        $("#yftf").val(yf);
                    }
                    $scope.buDan.yf_tf=$("#yftf").val();
                    $scope.buDan.yf_xf='';
                    $scope.buDan.yf_hf='';
                    $scope.buDan.yf_yj='';
                    $scope.buDan.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yfxf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $('#yfdf').removeAttr("readonly");
                        $('#yfdf').css("background-color","#fff");
                        $('#yf').removeAttr("readonly");
                        $('#yf').css("background-color","#fff");
                    }
                    break;
                case "3":
                    if(yf==0){
                        $("#yf").val($scope.buDan.yf);
                        $("#yfhf").val($scope.buDan.yf);
                    }
                    else {
                        $("#yf").val(yf);
                        $("#yfhf").val(yf);
                    }
                    $scope.buDan.yf_hf=$("#yfhf").val();
                    $scope.buDan.yf_xf='';
                    $scope.buDan.yf_tf='';
                    $scope.buDan.yf_yj='';
                    $scope.buDan.yf_ycdk='';
                    $("#yfxf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $('#yfdf').attr("readonly", "readonly");
                        $('#yfdf').css("background-color","#F2F2F2");
                        $scope.buDan.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
                case "4":
                    if(yf==0){
                        $("#yf").val($scope.buDan.yf);
                        $("#yfyj").val($scope.buDan.yf);
                    }
                    else {
                        $("#yf").val(yf);
                        $("#yfyj").val(yf);
                    }
                    $scope.buDan.yf_yj=$("#yfyj").val();
                    $scope.buDan.yf_xf='';
                    $scope.buDan.yf_tf='';
                    $scope.buDan.yf_hf='';
                    $scope.buDan.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfxf").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $('#yfdf').attr("readonly", "readonly");
                        $('#yfdf').css("background-color","#F2F2F2");
                        $scope.buDan.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
                case "5":
                    if(yf==0){
                        $("#yf").val($scope.buDan.yf);
                        $("#yfdk").val($scope.buDan.yf);
                    }
                    else {
                        $("#yf").val(yf);
                        $("#yfdk").val(yf);
                    }
                    $scope.buDan.yf_ycdk=$("#yfdk").val();
                    $scope.buDan.yf_xf='';
                    $scope.buDan.yf_tf='';
                    $scope.buDan.yf_yj='';
                    $scope.buDan.yf_hf='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfxf").val("");
                    if(ENV.fwqbs=='wh'){
                        $('#yfdf').attr("readonly", "readonly");
                        $('#yfdf').css("background-color","#F2F2F2");
                        $scope.buDan.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
            }
            if(fkType=="6"){
                $("#yun").removeClass("show").addClass("hide");
                $("#xfLab").removeClass("hide").addClass("showyf");
                $("#tfLab").removeClass("hide").addClass("showyf");
                $("#hfLab").removeClass("hide").addClass("showyf");
                $("#dkLab").removeClass("hide").addClass("showyf");
                $("#yjLab").removeClass("hide").addClass("showyf");
                if(ENV.fwqbs=='wh'){
                    if($scope.buDan.yf_xf!=''||$scope.buDan.yf_hf!=''||$scope.buDan.yf_ycdk!=''||$scope.buDan.yf_yj!=''){
                        $('#yfdf').attr("readonly", "readonly");
                        $('#yfdf').css("background-color","#F2F2F2");
                        $("#yfdf").val("");
                        $scope.buDan.yf_df='';
                    }
                    else {
                        $('#yfdf').removeAttr("readonly");
                        $('#yfdf').css("background-color","#fff");
                        $('#yf').removeAttr("readonly");
                        $('#yf').css("background-color","#fff");
                    }
                }

            }else {
                $("#yun").removeClass("hide").addClass("showyf");
                $("#xfLab").removeClass("show").addClass("hide");
                $("#tfLab").removeClass("show").addClass("hide");
                $("#hfLab").removeClass("show").addClass("hide");
                $("#dkLab").removeClass("show").addClass("hide");
                $("#yjLab").removeClass("show").addClass("hide");
            }
        });
        if (Storage.get('provincesData')!=null){
          $scope.provinces=Storage.get('provincesData');
        }else {
          FProCitAreFactory.getPro(sid,user_id,yh_id);
        }
        $scope.$on('FPro.proUpdated',function(){
          $scope.provinces=FProCitAreFactory.getP();
          Storage.set('provincesData',$scope.provinces);
        });
        $scope.$on('FCity.cityUpdated',function(){
            $scope.citiesF=FProCitAreFactory.getC();
            FProCitAreFactory.getAre(sid,user_id,yh_id,$scope.citiesF[0].code);
            Storage.set('citiesDataf',$scope.citiesF);
        });
        $scope.$on('FArea.areaUpdated',function(){
            $scope.areasF=FProCitAreFactory.getA();
            Storage.set('areasDataf',$scope.areasF);
        });
        $scope.$on('SCity.cityUpdated',function(){
            $scope.citiesS=SProCitAreFactory.getC();
            SProCitAreFactory.getAre(sid,user_id,yh_id,$scope.citiesS[0].code);
            Storage.set('citiesDatas',$scope.citiesS);
        });
        $scope.$on('SArea.areaUpdated',function(){
            $scope.areasS=SProCitAreFactory.getA();
            Storage.set('areasDatas',$scope.areasS);
        });
        $("#provincef").change(function(e) {
            FProCitAreFactory.getCity(sid,user_id,yh_id,$('#provincef').val());
            if ($('#provincef').val()=='地区选择'){
                $timeout(function(){
                    $scope.citiesF=null;
                    $scope.areasF=null;
                },300);
            }
        });
        $("#cityf").change(function(e) {
            FProCitAreFactory.getAre(sid,user_id,yh_id,$('#cityf').val());
        });
        $("#provinces").change(function(e) {
            SProCitAreFactory.getCity(sid,user_id,yh_id,$('#provinces').val());
            if ($('#provinces').val()=='地区选择'){
                $timeout(function(){
                    $scope.citiesS=null;
                    $scope.areasS=null;
                },300);
            }
        });
        $("#citys").change(function(e) {
            SProCitAreFactory.getAre(sid,user_id,yh_id,$('#citys').val());
        });

        function uploadImg(fhd_id){
            for (var i=0;i<$scope.imgData.length;i++){
                var canvas = document.createElement('CANVAS');
                var ctx = canvas.getContext('2d');
                var dataURL;
                var img = new Image();
                img.crossOrigin = 'Anonymous';
                img.src = $scope.imgData[i];
                img.onload = function(){
                    canvas.height = this.height;
                    canvas.width = this.width;
                    ctx.drawImage(this,0,0,this.width,this.height);
                    var ext = this.src.substring(this.src.lastIndexOf(".")+1).toLowerCase();
                    var extendName = this.src.substring(this.src.lastIndexOf("/")+1).toLowerCase();
                    dataURL = canvas.toDataURL("image/"+ext);
                    saveBdFactory.getSavaPic(sid,user_id,yh_id,dataURL,fhd_id,ext,extendName);
                };
            }

            $scope.$on('Save.picUpdated',function(){
                var result=saveBdFactory.getSavePicResult();
                if (result==1){

                }
            });
        }

        //保存补单
        $scope.saveCyd=function(){
            if( $scope.buDan.isCk==true){
                $scope.buDan.tbzt='1';
            }else {
                $scope.buDan.tbzt='';
            }
            if( $scope.buDan.isHd==true){
                $scope.buDan.sfhd='1';
            }else {
                $scope.buDan.sfhd='';
            }
            var fproCode=$('#provincef').val();
            var fcityCode=$('#cityf').val();
            var fareaCode=$('#areaf').val();
            var sproCode=$('#provinces').val();
            var scityCode=$('#citys').val();
            var sareaCode=$('#areas').val();
            var fpro=$('#provincef option:selected').text();
            var fcity=$('#cityf option:selected').text();
            var farea=$('#areaf option:selected').text();
            var spro=$('#provinces option:selected').text();
            var scity=$('#citys option:selected').text();
            var sarea=$('#areas option:selected').text();
            var shfs=$('#shfs option:selected').val();
            $("#saveBd").attr("disabled", true);
            Storage.set('buDan',null);
            //判断付款方式
            checkFkfs();
            if ($('#provincef option:selected').text()=='地区选择'){
              fproCode=0;
              fcityCode=0;
              fareaCode=0;
            }
            if ($('#provinces option:selected').text()=='地区选择'){
              sproCode=0;
              scityCode=0;
              sareaCode=0;
            }
            if ($scope.buDan.cydh==''){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "请输入承运单号",
                    duration: 600
                });
                $("#saveBd").attr("disabled", false);
                $timeout(function(){
                    $("#cydhCode").focus();
                },160);
            } else if ($scope.buDan.dwmc==''){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "请输入物流公司",
                  duration: 600
                });
                $("#saveBd").attr("disabled", false);
                $timeout(function(){
                  $("#dwmc").focus();
                },160);
            } else {
                saveBdFactory.checkCydhExist(sid,user_id,yh_id,$scope.buDan.cydh,$scope.buDan.dwmc);
                $scope.$on('Cydh.check',function(){
                  var result=saveBdFactory.getCount();
                  if (result==0){
                    saveBdFactory.getBd(sid,user_id,yh_id,$scope.buDan,$scope.cyrq,fpro,fcity,farea,spro,scity,sarea,fproCode,fcityCode,fareaCode,sproCode,scityCode,sareaCode,shfs,lxdh,3,3);
                  }else {
                    $ionicLoading.show({
                      noBackdrop: true,
                      template: "承运单号已存在！",
                      duration: 1500
                    });
                    $("#saveBd").attr("disabled", false);
                    $timeout(function(){
                      $("#cydhCode").focus();
                    },160);
                  }
                });
                $scope.$on('Save.bdUpdated',function(){
                    var result=saveBdFactory.getResult();
                    if (result.result==true){
                        $ionicLoading.show({
                            noBackdrop: true,
                            template: "保存成功",
                            duration: 600
                        });
                        $("#saveBd").attr("disabled", false);
                        $state.go('menu');
                        $timeout(function(){
                            uploadImg(result.fhd_id);
                        },160);
                    }
                });
            }
        };
        function checkFkfs(){
            //判断付款方式
            switch ($("#fkType option:selected").text()){
                case '现付':
                    $scope.buDan.yf_xf=$("#yf").val();
                    $scope.buDan.yf_hf='';
                    $scope.buDan.yf_tf='';
                    $scope.buDan.yf_yj='';
                    $scope.buDan.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $scope.buDan.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
                case '回付':
                    $scope.buDan.yf_hf=$("#yf").val();
                    $scope.buDan.yf_xf='';
                    $scope.buDan.yf_tf='';
                    $scope.buDan.yf_yj='';
                    $scope.buDan.yf_ycdk='';
                    $("#yfxf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $scope.buDan.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
                case '提付':
                    $scope.buDan.yf_tf=$("#yf").val();
                    $scope.buDan.yf_xf='';
                    $scope.buDan.yf_hf='';
                    $scope.buDan.yf_yj='';
                    $scope.buDan.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yfxf").val("");
                    $("#yfyj").val("");
                    $("#yfdk").val("");
                    break;
                case '月结':
                    $scope.buDan.yf_yj=$("#yf").val();
                    $scope.buDan.yf_xf='';
                    $scope.buDan.yf_hf='';
                    $scope.buDan.yf_tf='';
                    $scope.buDan.yf_ycdk='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfxf").val("");
                    $("#yfdk").val("");
                    if(ENV.fwqbs=='wh'){
                        $scope.buDan.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
                case '代扣':
                    $scope.buDan.yf_ycdk=$("#yf").val();
                    $scope.buDan.yf_xf='';
                    $scope.buDan.yf_hf='';
                    $scope.buDan.yf_tf='';
                    $scope.buDan.yf_yj='';
                    $("#yfhf").val("");
                    $("#yftf").val("");
                    $("#yfyj").val("");
                    $("#yfxf").val("");
                    if(ENV.fwqbs=='wh'){
                        $scope.buDan.yf_df='';
                        $("#yfdf").val("");
                    }
                    break;
                case '多笔付':
                    break;
            }
        };
        //$scope.imageSrc='img/login_bg.jpg';
        var imgPic=[];
        $scope.actionSheet=function(){
            var hideSheet = $ionicActionSheet.show({
                buttons: [
                    { text: '拍照' },
                    { text: '从相册选择' }
                ],
                cancelText: '取消',
                cancel: function() {
                    // add cancel code..
                },
                buttonClicked: function(index) {
                    if(index==0){
                        var options = {
                            destinationType: Camera.DestinationType.NATIVE_URI,
                            sourceType: Camera.PictureSourceType.CAMERA,
                            targetWidth: 800,
                            targetHeight: 800,
                        };

                        $cordovaCamera.getPicture(options).then(function(imageURI) {
                            if (imgPic.length>2){
                                $ionicLoading.show({
                                    noBackdrop: true,
                                    template: "最多支持三张照片",
                                    duration: 1500
                                });
                            }else {
                                imgPic[imgPic.length]= imageURI;
                            }
                        }, function(err) {
                            // error
                        });
                    }else if (index==1){
                        var options = {
                            maximumImagesCount: 10,
                            width: 800,
                            height: 800,
                            quality: 80
                        };

                        $cordovaImagePicker.getPictures(options)
                            .then(function (results) {
                                if (imgPic.length+results.length>3){
                                    $ionicLoading.show({
                                        noBackdrop: true,
                                        template: "最多支持三张照片",
                                        duration: 1500
                                    });
                                }else {
                                    for (var i = 0,j=imgPic.length; i < results.length; i++,j++) {
                                        imgPic[j]=results[i];
                                    }
                                }
                            }, function(error) {
                                // error getting photos
                            });
                    }
                    $scope.imgData=imgPic;

                    return true;
                }
            });
        };
        $scope.deleteImg=function(img){
            var j;
            var receipt = $ionicPopup.confirm({
                title : '是否删除图片',
                cancelText : '否',
                cancelType : 'button-stable',
                okText : '是'
            });
            receipt.then(function(r){
                if(r){
                    for (var i=0;i<$scope.imgData.length;i++){
                        if ($scope.imgData[i]==img){
                            j=i;
                            $scope.imgData.splice(j,1);
                        }
                    }
                }else{
                    console.log('取消');
                }
            });
        };

    })
    //选择联系人
    .controller('HzShrListCtrl',function($scope,$ionicLoading,$state,Storage,HzShrFactory,$ionicHistory,$stateParams,$ionicPopup,$window){
        $scope.showloading=true;
        var sid=Storage.get('sid');
        var user_id=Storage.get('user_id');
        var yh_id=Storage.get('yh_id');
        var htmlbs=$stateParams.htmlbs;
        var id=$stateParams.fhd_id;
        var khmc='';
        var lxdh='';
        var pyjm='';
        HzShrFactory.getcontent(sid,user_id,yh_id,khmc,lxdh,pyjm);
        $scope.$on('ShrList.shrUpdated',function(){
            var result=HzShrFactory.getShr();
            $scope.ShrListData=result.data;
            console.log($scope.ShrListData);
            $scope.showloading=false;
        });
        //下拉更新
        $scope.doRefresh=function(){
            HzShrFactory.getcontent(sid,user_id,yh_id,khmc,lxdh,pyjm);
            $scope.$broadcast('scroll.refreshComplete');
        };
        //上拉更新
        $scope.loadMore=function(){
            HzShrFactory.getMoreShr(sid,user_id,yh_id,khmc,lxdh,pyjm);
            $scope.$broadcast('scroll.infiniteScrollComplete');
        };
        //下一页
        $scope.hasNextPage = function() {
            return HzShrFactory.hasNextPage();
        };

        $scope.search={
            content:''
        };

        $scope.searchLinkman=function(val){
            var re = /^[0-9]+.?[0-9]*$/;
            var reg= /^[A-Za-z]+$/;
            if (re.test(val)){
                lxdh=val;
            }else if (reg.test(val)){
                pyjm=val.toUpperCase();
            }else {
                khmc=val;
            }
            HzShrFactory.getcontent(sid,user_id,yh_id,khmc,lxdh,pyjm);
        };

        $scope.chooseLinkman=function(khmc,lxdh,address){
            if(htmlbs==1){
                $state.go('hzKaiDan',{khmc:khmc,lxdh:lxdh,address:address});
            }else if(htmlbs==2){
                $state.go('hzBuDan',{khmc:khmc,lxdh:lxdh,address:address});
            }else if (htmlbs==3){
                $state.go('hzChangeDan',{fhd_id:id,khmc:khmc,lxdh:lxdh,address:address});
            }else if(htmlbs==4){
                $state.go('changeBuDan',{fhd_id:id,khmc:khmc,lxdh:lxdh,address:address});
            }
        };
        //长按删除
        $scope.deleteLink=function(data){
            var receipt = $ionicPopup.confirm({
                title : '是否删除联系人',
                cancelText : '取消',
                cancelType : 'button-stable',
                okText : '删除'
            });
            receipt.then(function(r){
                if(r){
                    HzShrFactory.getDelete(sid,user_id,yh_id,data.id);
                    $scope.$on('DeleteData',function(){
                        var result=HzShrFactory.deleteResult();
                        if (result==1){
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "删除成功",
                                duration: 1500
                            });
                            $scope.ShrListData.splice($scope.ShrListData.indexOf(data), 1);
                        }else {
                            $ionicLoading.show({
                                noBackdrop: true,
                                template: "删除失败",
                                duration: 1500
                            });
                        }
                    });
                }else{
                    console.log('取消');
                }
            });
        };
        //拨号
        $scope.callPhone = function (mobilePhone) {
            var confirmPopup = $ionicPopup.confirm({
                title:mobilePhone,
                okText:'呼叫',
                cancelText:'取消'
            });
            confirmPopup.then(function(res) {
                if (res) {
                    $window.location.href = "tel:" + mobilePhone;
                }else {

                }
            });
        };
        $scope.myGoBack = function() {
            $ionicHistory.goBack();
        };
    });
